import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
import '../../core/elegance_theme.dart';

/// 卡片阴影显示时机枚举
enum CardShadowType {
  /// 始终显示阴影
  always,
  
  /// 鼠标悬停时显示阴影
  hover,
  
  /// 从不显示阴影
  never,
}

/// 卡片组件
class ECard extends StatefulWidget {
  /// 创建卡片组件
  const ECard({
    super.key,
    this.title,
    this.subtitle,
    this.body,
    this.footer,
    this.headerIcon,
    this.cornerRadius,
    this.shadowType = CardShadowType.always,
    this.elevation = 2.0,
    this.header,
    this.leftTop,
    this.rightTop,
    this.leftBottom,
    this.rightBottom,
    this.padding = const EdgeInsets.all(16.0),
    this.margin = EdgeInsets.zero,
    this.backgroundColor,
    this.borderColor,
    this.headerColor,
    this.bodyColor,
    this.footerColor,
    this.width,
    this.height,
  });

  /// 卡片标题
  final String? title;
  
  /// 卡片副标题
  final String? subtitle;
  
  /// 卡片内容区域
  final Widget? body;
  
  /// 卡片页脚
  final Widget? footer;
  
  /// 左上角图标
  final Widget? headerIcon;
  
  /// 圆角大小
  final double? cornerRadius;
  
  /// 阴影显示时机
  final CardShadowType shadowType;
  
  /// 阴影高度
  final double elevation;
  
  /// 自定义头部组件
  final Widget? header;
  
  /// 左上角自定义组件（会覆盖headerIcon）
  final Widget? leftTop;
  
  /// 右上角自定义组件
  final Widget? rightTop;
  
  /// 左下角自定义组件
  final Widget? leftBottom;
  
  /// 右下角自定义组件
  final Widget? rightBottom;
  
  /// 内边距
  final EdgeInsets padding;
  
  /// 外边距
  final EdgeInsets margin;
  
  /// 背景颜色
  final Color? backgroundColor;
  
  /// 边框颜色
  final Color? borderColor;
  
  /// 头部背景颜色
  final Color? headerColor;
  
  /// 内容区域背景颜色
  final Color? bodyColor;
  
  /// 页脚背景颜色
  final Color? footerColor;
  
  /// 卡片宽度
  final double? width;
  
  /// 卡片高度
  final double? height;
  
  @override
  State<ECard> createState() => _ECardState();
}

class _ECardState extends State<ECard> {
  bool _isHovered = false;
  
  @override
  Widget build(BuildContext context) {
    final theme = EleganceTheme.of(context);
    final borderRadius = widget.cornerRadius ?? 4.0;
    
    // 确定是否显示阴影
    bool shouldShowShadow() {
      switch (widget.shadowType) {
        case CardShadowType.always:
          return true;
        case CardShadowType.hover:
          return _isHovered;
        case CardShadowType.never:
          return false;
        default:
          return true;
      }
    }
    
    // 构建头部区域
    Widget _buildHeader() {
      if (widget.header != null) {
        return Container(
          padding: widget.padding,
          color: widget.headerColor ?? widget.backgroundColor,
          child: widget.header,
        );
      }
      
      if (widget.title == null && widget.subtitle == null && widget.headerIcon == null) {
        return Container();
      }
      
      return Container(
        padding: widget.padding,
        color: widget.headerColor ?? widget.backgroundColor,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                // 头部图标
                if (widget.headerIcon != null && widget.leftTop == null) ...[
                  Padding(
                    padding: const EdgeInsets.only(right: 8.0),
                    child: widget.headerIcon,
                  ),
                ],
                // 标题和副标题
                if (widget.title != null || widget.subtitle != null) ...[
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      if (widget.title != null) ...[
                        Text(
                          widget.title!,
                          style: TextStyle(
                            fontSize: 16.0,
                            fontWeight: FontWeight.bold,
                            color: theme.textPrimaryColor,
                          ),
                        ),
                      ],
                      if (widget.subtitle != null) ...[
                        if (widget.title != null) const SizedBox(height: 4.0),
                        Text(
                          widget.subtitle!,
                          style: TextStyle(
                            fontSize: 14.0,
                            color: theme.textSecondaryColor,
                          ),
                        ),
                      ],
                    ],
                  ),
                ],
              ],
            ),
            // 右上角自定义组件
            if (widget.rightTop != null) widget.rightTop!,
          ],
        ),
      );
    }
    
    // 构建内容区域
    Widget _buildBody() {
      if (widget.body == null) return Container();
      
      return Container(
        padding: widget.padding,
        color: widget.bodyColor ?? widget.backgroundColor,
        child: widget.body,
      );
    }
    
    // 构建页脚区域
    Widget _buildFooter() {
      if (widget.footer == null) return Container();
      
      return Container(
        padding: widget.padding,
        color: widget.footerColor ?? widget.backgroundColor,
        child: widget.footer,
      );
    }
    
    // 构建卡片内容
    List<Widget> _buildCardContent() {
      final List<Widget> content = [];
      
      final header = _buildHeader();
      if (header is! Container || header.child != null) {
        content.add(header);
        content.add(Divider(height: 1.0, color: widget.borderColor ?? theme.borderColor));
      }
      
      final body = _buildBody();
      if (body is! Container || body.child != null) {
        content.add(body);
        content.add(Divider(height: 1.0, color: widget.borderColor ?? theme.borderColor));
      }
      
      final footer = _buildFooter();
      if (footer is! Container || footer.child != null) {
        content.add(footer);
      }
      
      // 如果内容不为空但最后一项是Divider，移除它
      if (content.isNotEmpty && content.last is Divider) {
        content.removeLast();
      }
      
      return content;
    }
    
    // 构建卡片容器
    Widget cardContainer = Container(
      width: widget.width,
      height: widget.height,
      margin: widget.margin,
      decoration: BoxDecoration(
        color: widget.backgroundColor ?? theme.surfaceColor,
        borderRadius: BorderRadius.circular(borderRadius),
        border: Border.all(
          color: widget.borderColor ?? theme.borderColor,
          width: 1.0,
        ),
        boxShadow: shouldShowShadow()
            ? [
                BoxShadow(
                  color: Colors.black.withOpacity(0.1),
                  spreadRadius: 1.0,
                  blurRadius: widget.elevation,
                  offset: const Offset(0, 2),
                ),
              ]
            : [],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        mainAxisSize: MainAxisSize.max,
        children: _buildCardContent(),
      ),
    );
    
    // 根据阴影类型添加鼠标悬停效果
    if (widget.shadowType == CardShadowType.hover) {
      cardContainer = MouseRegion(
        onEnter: (_) => setState(() => _isHovered = true),
        onExit: (_) => setState(() => _isHovered = false),
        child: cardContainer,
      );
    }
    
    // 构建带角落自定义组件的卡片
    return Stack(
      children: [
        cardContainer,
        // 左上角自定义组件
        if (widget.leftTop != null) ...[
          Positioned(
            top: 0,
            left: 0,
            child: Container(
              padding: const EdgeInsets.all(8.0),
              child: widget.leftTop,
            ),
          ),
        ],
        // 右上角自定义组件
        if (widget.rightTop != null && widget.header == null) ...[
          Positioned(
            top: 0,
            right: 0,
            child: Container(
              padding: const EdgeInsets.all(8.0),
              child: widget.rightTop,
            ),
          ),
        ],
        // 左下角自定义组件
        if (widget.leftBottom != null) ...[
          Positioned(
            bottom: 0,
            left: 0,
            child: Container(
              padding: const EdgeInsets.all(8.0),
              child: widget.leftBottom,
            ),
          ),
        ],
        // 右下角自定义组件
        if (widget.rightBottom != null) ...[
          Positioned(
            bottom: 0,
            right: 0,
            child: Container(
              padding: const EdgeInsets.all(8.0),
              child: widget.rightBottom,
            ),
          ),
        ],
      ],
    );
  }
}